<?php
declare (strict_types=1);

namespace app\api\controller\user;

use app\admin\model\freight\ExpressCity;
use app\admin\model\UserAddress;
use app\api\controller\Base;
use app\common\service\user\AddressService;
use app\validate\Site;
use think\exception\ValidateException;
use think\Request;

class BindAddress extends Base
{

    /**
     * 地址添加
     * Author:YangXinPeng
     * @return mixed
     */
    public function add()
    {
        $params = $this->request->post();
        $params['province_region'] = $params['value'][0];
        $params['city_region'] = $params['value'][1];
        $params['district_region'] = $params['value'][2];
        $uid = $this->request->uid;
        try {
            validate(Site::class)->check([
                'name' => $params['name'],
                'phone' => $params['phone'],
                'province_region' => $params['province_region'],
                'city_region' => $params['city_region'],
                'district_region' => $params['district_region'],
                'detailed_address' => $params['detailed_address'],
            ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $data = ['code' => 400, 'message' => $e->getError()];
            return $this->res($data);
        }
        $params['uid'] = $uid;
        $data = AddressService::addSite($params);
        return $this->res($data);
    }


    /**
     * Author:YangXinPeng
     * 地址列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function address()
    {
        $uid = $this->request->uid;
        $res = AddressService::show($uid);
        foreach ($res['data'] as $key => $value) {
            $province = ExpressCity::where('id', $value['province_region'])->value('city_name');
            $city = ExpressCity::where('id', $value['city_region'])->value('city_name');
            $district = ExpressCity::where('id', $value['district_region'])->value('city_name');
            $res['data'][$key]['address'] = $province . $city . $district . $value['detailed_address'];
        }
        return $this->res($res);
    }

    /**
     * Author:YangXinPeng
     * 根据ID查询地址
     * @param Request $request
     * @return mixed
     */
    public function addressToId(Request $request)
    {
        $eid = $request->get('eid');
        $data = AddressService::findById($eid);
        return $this->res($data);
    }

    public function addressDetail(Request $request)
    {
        $eid = $request->get('eid');
        $data = AddressService::one($eid);
        return $this->res($data);
    }

    /**
     * Author:YangXinPeng
     * 获取该用户默认地址
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function defaultAddress()
    {
        $uid = $this->request->uid;
        $where[] = ['uid', '=', $uid];
        $where[] = ['is_default', '=', 1];
        $data = UserAddress::where($where)->find();
        $data && $data = $data->toArray();
        $province = ExpressCity::where('id', $data['province_region'])->value('city_name');
        $city = ExpressCity::where('id', $data['city_region'])->value('city_name');
        $district = ExpressCity::where('id', $data['district_region'])->value('city_name');
        $res['address'] = $province . $city . $district . $data['detailed_address'];
        $res['name'] = $data['name'];
        $res['phone'] = $data['phone'];
        $res['id'] = $data['id'];
        return json($res);
    }

}
